import 'package:flutter/cupertino.dart';

import 'package:get/get.dart';

import '../modules/campus/campus/bindings/campus_binding.dart';
import '../modules/campus/campus/views/campus_view.dart';
import '../modules/campus/random/bindings/random_binding.dart';
import '../modules/campus/random/views/random_view.dart';
import '../modules/group_info/bindings/group_info_binding.dart';
import '../modules/group_info/views/group_info_view.dart';
import '../modules/home/bindings/home_binding.dart';
import '../modules/home/views/home_view.dart';
import '../modules/login/bindings/login_binding.dart';
import '../modules/login/views/login_view.dart';
import '../modules/me/edit_me/bindings/edit_me_binding.dart';
import '../modules/me/edit_me/views/edit_me_view.dart';
import '../modules/me/me/bindings/me_binding.dart';
import '../modules/me/me/views/me_view.dart';
import '../modules/message/bindings/message_binding.dart';
import '../modules/message/views/message_view.dart';
import '../modules/msg_list/bindings/msglist_binding.dart';
import '../modules/msg_list/views/msglist_view.dart';
import '../modules/other_profile/bindings/other_profile_binding.dart';
import '../modules/other_profile/views/other_profile_view.dart';
import '../modules/sign_up/bindings/sign_up_binding.dart';
import '../modules/sign_up/views/sign_up_view.dart';
import '../service/local_storage_service.dart';

part 'app_routes.dart';

class AppPages {
  AppPages._();

  static const INITIAL = Routes.HOME;

  static final routes = [
    GetPage(
        name: _Paths.HOME,
        page: () => HomeView(),
        binding: HomeBinding(),
        middlewares: [
          new RouteAuthMiddleware(priority: 1)
        ],
        children: [
          GetPage(
            name: _Paths.MSG_LIST,
            page: () => MsgListView(),
            binding: MsgListBinding(),
          ),
          GetPage(
            name: _Paths.ME,
            page: () => MeView(),
            binding: MeBinding(),
          ),
        ]),
    GetPage(
      name: _Paths.CAMPUS,
      page: () => CampusView(),
      binding: CampusBinding(),
      children: [
        GetPage(
          name: _Paths.RANDOM,
          page: () => RandomView(),
          binding: RandomBinding(),
        ),
      ],
    ),
    GetPage(
      name: _Paths.LOGIN,
      page: () => LoginView(),
      binding: LoginBinding(),
    ),
    GetPage(
      name: _Paths.MESSAGE,
      page: () => MessageView(),
      binding: MessageBinding(),
    ),
    GetPage(
      name: _Paths.EDIT_ME,
      page: () => EditMeView(),
      binding: EditMeBinding(),
    ),
    GetPage(
      name: _Paths.OTHER_PROFILE,
      page: () => OtherProfileView(),
      binding: OtherProfileBinding(),
    ),
    GetPage(
      name: _Paths.SIGN_UP,
      page: () => SignUpView(),
      binding: SignUpBinding(),
    ),
    GetPage(
      name: _Paths.GROUP_INFO,
      page: () => GroupInfoView(),
      binding: GroupInfoBinding(),
    ),
  ];
}

class RouteAuthMiddleware extends GetMiddleware {
  LocalStorageService localStorageService = Get.find();
  @override
  int? priority = 0;

  RouteAuthMiddleware({required this.priority});

  @override
  RouteSettings? redirect(String? route) {
    var str = localStorageService.getData(localStorageService.userInfoKey);
    if (str == null) {
      Future.delayed(Duration(seconds: 1), () => Get.snackbar("提示", "请先登录APP"));
      return RouteSettings(name: Routes.LOGIN);
    }
    return null;
  }
}
